# Copyright (C) 2006  Alex Schroeder <alex@emacswiki.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
#    Free Software Foundation, Inc.
#    59 Temple Place, Suite 330
#    Boston, MA 02111-1307 USA

sub GetPageOrEditLink {
    my ($id, $text, $bracket, $free) = @_;
    $id = FreeToNormal($id);
    my ($class, $resolved, $title, $exists) = ResolveId($id);
    if (!$text && $resolved && $bracket) {
        $text = BracketLink(++$FootnoteNumber); # s/_/ /g happens further down!
        $class .= ' number';
        $title = $id; # override title
        $title =~ s/_/ /g if $free;
    }
    $text = "[$id]" if not $text and $bracket; # if the page exists with brackets and no text see above
    $text = $id if not $text;
    $text =~ s/_/ /g;
    if ($resolved) { # anchors don't exist as pages, therefore do not use $exists
        if ($class =~ /near/ ) {
            return ScriptLink(UrlEncode($resolved), $text, $class, undef, T('On ' . $title .'Wiki : ' . unCamel($id)));
        } else {
            return ScriptLink(UrlEncode($resolved), $text, $class, undef, T('Internal page: ' . unCamel($id)));
        }
    } else {
        return GetEditLink($id, $text);
    }
}

#sub GetDownloadLink {
#    my ($name, $image, $revision, $alt) = @_;
#    $alt = $name unless $alt;
#    $alt =~ s/_/ /g;
#    my $id = FreeToNormal($name);
#    # if the page does not exist
#    return GetEditLink($id, ($image ? T('image') : T('download')) . ':' . $name, 1)
#    unless $IndexHash{$id};
#    my $action;
#    if ($revision) {
#        $action = "action=download;id=" . UrlEncode($id) . ";revision=$revision";
#    } elsif ($UsePathInfo) {
#        $action = "download/" . UrlEncode($id);
#    } else {
#        $action = "action=download;id=" . UrlEncode($id);
#    }
#    if ($image) {
#        if ($UsePathInfo and not $revision) {
#            $action = $ScriptName . '/' . $action;
#        } else {
#            $action = $ScriptName . '?' . $action;
#        }
#        my $result = $q->img({-src=>$action, -alt=>$alt, -class=>'upload'});
#        $result = ScriptLink(UrlEncode($id), $result, 'image') unless $id eq $OpenPageName;
#        return $result;
#    } else {
#        return ScriptLink($action, $alt, 'upload');
#    }
#}

# Integrate into wiki code
*OldGetEditLink = *GetEditLink;
*GetEditLink = *CustomGetEditLink;

sub CustomGetEditLink { # shortcut
    my ($id, $name, $upload, $accesskey) = @_;
    $id = FreeToNormal($id);
    $name =~ s/_/ /g;
    my $action = 'action=edit;id=' . UrlEncode($id);
    $action .= ';upload=1' if $upload;
    return ScriptLink($action, $name, 'edit', undef, T('Create a new page: "' . unCamel($id) . "\""), $accesskey);
    #return $name . ScriptLink($action, "?", 'edit', undef, T('Create a new page named: ' . $id ), $accesskey);
}

# Integrate back into wiki code
*OldGetUrl = *GetUrl;
*GetUrl = *CustomGetUrl;

sub CustomGetUrl {
    my ($url, $text, $bracket, $images) = @_;
    $url =~ /^($UrlProtocols)/;
    my $class = "url $1";
    if ($NetworkFile && $url =~ m|^file:///| && !$AllNetworkFiles or !$NetworkFile && $url =~ m|^file:|) {
        # Only do remote file:// links. No file:///c|/windows.
        return $url;
    } elsif ($bracket && !$text) {
        $text = BracketLink(++$FootnoteNumber);
        $class .= ' number';
    } elsif (!$text) {
        $text = $url;
    } elsif ($bracket) { # and $text is set
        $class .= ' outside';
    }
    $url = UnquoteHtml($url); # links should be unquoted again
    if ($images && $url =~ /^(http:|https:|ftp:).+\.$ImageExtensions$/i) {
        return $q->img({-src=>$url, -alt=>$url, -class=>$class});
    } else {
        return $q->a({-href=>$url, -title=>"External Link to: $text" , -class=>$class}, $text);
    }
}

# Integrate back into wiki code
*OldGetInterLink = *GetInterLink;
*GetInterLink = *CustomGetInterLink;

sub CustomGetInterLink {
  my ($id, $text, $bracket, $quote) = @_;
  my ($site, $page) = split(/:/, $id, 2);
  $page =~ s/&amp;/&/g;  # Unquote common URL HTML
  my $url = GetInterSiteUrl($site, $page, $quote);
  my $class = 'inter ' . $site;
  if ($text && $bracket && !$url) {
    return "[$id $text]";
  } elsif ($bracket && !$url) {
    return "[$id]";
  } elsif (!$url) {
    return $id;
  } elsif ($bracket && !$text) {
    $text = BracketLink(++$FootnoteNumber);
    $class .= ' number';
  } elsif (!$text) {
    $text = $q->span({-class=>'site'}, $site) . ':' . $q->span({-class=>'page'}, $page);
  } elsif ($bracket) { # and $text is set
    $class .= ' outside';
  }
  return $q->a({-href=>$url, -class=>$class, -title=>'External Reference: '. $site}, $text);
}

#
# Prepare Nearlinks for appearance in sidebar...
#

#*OldGetNearLinksUsed = *GetNearLinksUsed;
#*GetNearLinksUsed = *CustomGetNearLinksUsed;

sub CustomGetNearLinksUsed {
    my $result;
    if (%NearLinksUsed) {
        $result .= $q->start_div({-class=>'sideblock'});
        $result .= $q->p({-class=>'sidetitle'},T('Edit instead of Auto Link'));
        $result .= $q->div({-class=>'near'}, map { GetEditLink($_, $_); } keys %NearLinksUsed);
        $result .= $q->end_div();
        return $result;
    }
    return '';
}

#
# Prepare Sisterlinks for appearance in sidebar and remove images...
#

#*OldGetSisterSites = *GetSisterSites;
#*GetSisterSites = *CustomGetSisterSites;

sub CustomGetSisterSites {
    my $id = shift;
    my $sisters = "";
    if ($id and $NearSource{$id}) {
        $result .= $q->start_div({-class=>'sideblock'});
        $result .= $q->p({-class=>'sidetitle'},T('On Other Wikis'));
        foreach my $site (@{$NearSource{$id}}) {
            $sisters .= $q->a({-href=>GetInterSiteUrl($site, $id), -title=>$id . ' on ' . $site},,"$site:$id");
        }
        $result .= $q->p($sisters);
        $result .= $q->end_div();
        return $result;
    }
    return '';
}
1;
